package dhm.com.dhmshop.updata.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.InvoiceAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.InvoiceBean;
import dhm.com.dhmshop.utils.SpUtils;

public class InvoiceActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.recy_message)
    XRecyclerView recyMessage;

    private PressenterImpl pressenter;

    int page = 1;
    private List<InvoiceBean.DataBean> invoiceBeans;
    private InvoiceAdapter invoiceAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_invoice;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        page = 1;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        invoiceAdapter = new InvoiceAdapter(this);
        String uid = SpUtils.getString(this, "uid");
        invoiceAdapter.setdeleteClick(new InvoiceAdapter.OnClick() {
            @Override
            public void item(int posttion) {
                Map<String, String> map = new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("id", invoiceBeans.get(posttion).getId()+"");
                pressenter.sendMessage(InvoiceActivity.this, Constant.InvoiceDel, map, Bean.class);
            }
        });
        recyMessage.setPullRefreshEnabled(true);
        recyMessage.setLoadingMoreEnabled(true);
        recyMessage.setLayoutManager(manager);
        recyMessage.setAdapter(invoiceAdapter);

        recyMessage.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getdata();
            }

            @Override
            public void onLoadMore() {
                getdata();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        page=1;
        getdata();
    }

    private void getdata() {
        String uid = SpUtils.getString(this, "uid");
        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid", uid);
        map.put("page", page + "");
        pressenter.sendMessage(this, Constant.GetInvoiceList, map, InvoiceBean.class);
    }

    @Override
    protected void initData() {

    }


    @Override
    public void requesta(Object data) {
        if (data instanceof Bean) {
            Bean bean = (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (((Bean) data).getCode() == 1) {
                page = 1;
                getdata();
            }
        } else if (data instanceof InvoiceBean) {
            InvoiceBean invoiceBean = (InvoiceBean) data;
            if (page == 1) {
                invoiceBeans = invoiceBean.getData();
            } else {
                invoiceBeans.addAll(invoiceBean.getData());
            }

            invoiceAdapter.setShopList(invoiceBeans);
            page++;
            recyMessage.refreshComplete();
            recyMessage.loadMoreComplete();
        }
    }

    @Override
    public void fail(String error) {

    }


    @OnClick({R.id.back, R.id.add_invoice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_invoice:
                Intent intent = new Intent(InvoiceActivity.this, InvoiceaddActivity.class);
                intent.putExtra("type","0");
                startActivity(intent);
                break;
            default:
        }
    }
}
